package com.zara.store.common;

import java.util.Collection;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.zara.store.common.EnvtVO;
import com.zara.store.common.InvoiceVO;
import com.zara.store.common.OfadVO;
import com.zara.store.common.PalcItemVO;
import com.zara.store.common.PalcItemViewVO;
import com.zara.store.common.PalcVO;
import com.zara.store.common.ProductVO;
import com.zara.store.common.StoreFacade;

public class StoreProxy {
	
	private StoreFacade storeFacade;
	private String naming = "zara.store/StoreFacadeBean/remote";

	private static StoreProxy instance = new StoreProxy();
	
	public static StoreProxy getInstance() {
		return instance;
	}
	
	public StoreProxy() {
		InitialContext ic;
		try {
//			Properties properties = new Properties();
//			properties.load(StoreProxy.class.getResourceAsStream("/jndi.properties"));
//			java.util.Hashtable<String, String> properties = new java.util.Hashtable<String, String>();
////			properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
////			properties.put("java.naming.provider.url", "jnp://127.0.0.1:1099");
//			ic = new InitialContext(properties);
			ic = new InitialContext();
			System.out.println("ic: " + ic);
			storeFacade = (StoreFacade) ic.lookup(naming);
			System.out.println("sf: " + storeFacade);
		} catch (NamingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (RuntimeException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
			throw e;
		}
	}
	
	public ProductVO queryProduct(String barcode) {
		return storeFacade.queryProduct(barcode);
	}
	
	public InvoiceVO createInvoice(InvoiceVO invoice) {
		System.out.println("sf createInvoice");
		return storeFacade.createInvoice(invoice);
	}

	public Collection<PalcItemViewVO> queryPalcProductList() {
		return storeFacade.queryPalcProductList();
	}

	public PalcVO createPalc(List<PalcItemVO> order) {
		return storeFacade.createPalc(order);
	}

	public OfadVO createOfad(OfadVO ofadVO) {
		return storeFacade.createOfad(ofadVO);
	}

	public EnvtVO createEnvt(EnvtVO envtVO) {
		return storeFacade.createEnvt(envtVO);
	}

	public EnvtVO createEnvtFromXml(String xml) {
		//return storeFacade.createEnvtFromXml(xml);
		return null;
	}

}
